define(function(require, exports, module) {
  var $ = require('$');
  var nav = require("bootstrap_nav");
  // var form_tpl = require("./phonebilldetail.tpl");
  var form_tpl_callingnum = require("./callingnum.tpl");
  var Widget = require("widget");
  // var nav = require("bootstrap_nav");

  require("bootstrap_commonrequire");

  require("moment");
  require("bootstrap-daterangepicker");
  require("daterangepicker.css");

  exports.init = function(backto, param, rowData, flagIsv, startIndex) {
    // 获取隐藏域中的查询条件
    var _carrierid = $('#_carrierid').val();
    var _carriertext = $('#_carriertext').val();
    var _isvid = $('#_isvid').val();
    var _applyid = $('#_applicationid').val();
    var _sessionid = $('#_sessionid').val();
    var _starttimes = $('#_starttimes').val();
    var _endtimes = $('#_endtimes').val();

    var DetailWindow = Widget.extend({
      // 初始化参数
      initCustAttr : function() {
        // 输入项初始化
        var attrs = this.get("attrs");
      },
      template : form_tpl_callingnum
    });
    var mybuttons = null;
    mybuttons = [ {
      text : "返回",
      id : "btn_back",
      style : "btn btn-contrast",
      type : "button",
      icon : "undo"
    } ];
    new DetailWindow({
      attrs : {
        title : "通话详单",
        // contitle : "主叫号码信息",
        spansize : 'span12',
        id : "callingnum",
        icon : "icon-credit-card",
        items : [ {
          colums : [ {
            label : "主叫号码",
            name : 'callingnum',
            value : rowData.callingnum
          }, {
            label : "会话ID",
            name : 'sessionid',
            value : rowData.sessionid

          } ]
        }, {
          colums : [ {
            label : "总时长(分钟)",
            name : 'callingduration',
            value : rowData.callingduration
          }, {
            label : "总费用(元)",
            name : 'callingcost',
            value : rowData.callingcost
          } ]
        } ],
        otherCons : [ {
          id : param,
          // contitle : "被叫号码详细信息",
          spansize : "span12"
        } ],
        buttons : mybuttons
      },
      renderTo : "content-wrapper",
      afterRender : function() {
        $('#btn_back').on('click', function() {
          //将数据放入页面缓存中
          localStorage.setItem("_carrierid", _carrierid);
          localStorage.setItem("_carriertext", _carriertext);
          localStorage.setItem("_isvid", _isvid);
          localStorage.setItem("_applyid", _applyid);
          localStorage.setItem("_sessionid", _sessionid);
          localStorage.setItem("_starttimes", _starttimes);
          localStorage.setItem("_endtimes", _endtimes);
          localStorage.setItem("_start", startIndex);
          nav.toPageView(backto);
        });
      }
    });

    // 加载表单模板和数据
    var Table = require("table");
    new Table({
      attrs : {
        id : "datatableid",
        isHovered : false,
        isStriped : true,
        isBordered : false
      },
      renderTo : param,
      afterRender : function() {
        var dtUtil = require('datatableutil');
        $('#datatableid').dataTable(
            {
              "bJQueryUI" : false,
              "oLanguage" : {
                "sUrl" : "/business/js/common/table/src/lang/dataTable.cn.txt"
              },
              "sPaginationType" : "bootstrap", // 分页自带两种样式，full_numbers和two_button(默认)
              "scrollX" : false,
              'bPaginate' : false, // 是否分页
              "bProcessing" : false, // 当datatable获取数据时候是否显示正在处理提示信息。
              "bServerSide" : false, // 指定服务器端分页
              "bDeferRender" : false,
              "bFilter" : false,
              "bInfo" : false,
              "sAjaxSource" : "/data/queryPhoneBillBySessionId.do",// GLOBAL.URL.ADMIN_GETRETLICENSECARRIERS+carrierlicId
                                                                    // + ".do",
              // 数据源
              "fnServerData" : function(sSource, aoData, fnCallback, oSettings) {

                var queryparam = dtUtil.convert_aoData(aoData);
                // 拼接where查询条件
                var cond = rowData.sessionid;
                if (cond != '' && cond != null) {
                  queryparam.q += cond;
                }

                // 添加自己的额外参数
                oSettings.jqXHR = $.ajax({
                  "dataType" : 'json',
                  "contentType" : "application/json;charset=UTF-8",
                  "type" : "GET",
                  "url" : sSource,
                  "data" : queryparam, // 将请求数据转换为后台要求的JSON格式
                  "success" : function(json) {
                    dtUtil.successCallback(json, aoData, fnCallback);
                  }
                });

              },

              "aoColumns" : [
                  {
                    "sTitle" : "被叫号码",
                    "bSortable" : false,
                    "mData" : "callee"
                  },
                  {
                    "sTitle" : "通话开始时间",
                    "bSortable" : false,
                    "mData" : "beginTime",
                    "mRender" : function(data, type, full) {

                      var retData = data.substr(0, 4) + "-" + data.substr(4, 2) + "-" + data.substr(6, 2) + " "
                          + data.substr(8, 2) + ":" + data.substr(10, 2) + ":" + data.substr(12, 2);

                      return retData;

                    }
                  },
                  {
                    "sTitle" : "通话结束时间",
                    "bSortable" : false,
                    "mData" : "endTime",
                    "mRender" : function(data, type, full) {

                      var retData = data.substr(0, 4) + "-" + data.substr(4, 2) + "-" + data.substr(6, 2) + " "
                          + data.substr(8, 2) + ":" + data.substr(10, 2) + ":" + data.substr(12, 2);

                      return retData;

                    }

                  }, {
                    "sTitle" : "通话时长(分钟)",
                    "bSortable" : false,
                    "mData" : "time",
                    "mRender" : function(data, type, full) {
                      return data % 60 == 0 ? (data / 60) : Math.ceil(data / 60);
                    }
                  } ]
            });
      }
    });

  };
});
